#include <stdlib.h>
#include <stdio.h>
#include "func.h"


int main(int argc, char *argv[])
{
	int sock, sockmsg, length, lengthmsg;
	char client_cmd[10], cmd_arg[20];
	struct sockaddr_in server;
	struct sockaddr_in server_msg;
	int datasock, msgsock;
	pid_t child;
	int rval;

	sock = socket(AF_INET, SOCK_STREAM, 0);
	sockmsg = socket(AF_INET, SOCK_STREAM, 0);
	if (sock < 0 || sockmsg < 0) {
		perror("Opening stream socket err");
		exit(EXIT_FAILURE);
	}

	server.sin_family = AF_INET;
	server.sin_port = htons(LISTEN_PORT);
	server.sin_addr.s_addr = htonl(INADDR_ANY);

	server_msg.sin_family = AF_INET;
	server_msg.sin_port = htons(LISTEN_PORT + 1);
	server_msg.sin_addr.s_addr = htonl(INADDR_ANY);

	length = sizeof(server);
	lengthmsg = sizeof(server_msg);
	if (bind(sock, (struct sockaddr *)&server, (socklen_t)length) < 0 ||
			bind(sockmsg, (struct sockaddr *)&server_msg, (socklen_t)lengthmsg)< 0) {
		perror("Binding stream socket err");
		exit(EXIT_FAILURE);
	}

	//getsockname获得当前连接的地址
	if (getsockname(sock, (struct sockaddr *)&server, (socklen_t *)&length) < 0 ||
				getsockname(sockmsg, (struct sockaddr *)&server_msg, 
					(socklen_t *)&lengthmsg) < 0) {
		perror("getting sock name err");
		exit(1);
	}
	
	printf("Sock port $ %d	%d\n", ntohs(server.sin_port), ntohs(server_msg.sin_port));

	memset(currentDirPath, 0, sizeof(currentDirPath));
	getcwd(currentDirPath, sizeof(currentDirPath));		//获得当前目录

	listen(sock, 5);
	listen(sockmsg, 5);

	do {
		datasock = accept(sock, (struct sockaddr *)0, (socklen_t *)0);
		msgsock = accept(sockmsg, (struct sockaddr *)0, (socklen_t *)0);
		if (datasock == -1 || msgsock == -1) {
			perror("accept err");
			exit(1);
		}
		else {
			if ((child = fork()) == -1) {
				perror("fork err");
				exit(1);
			}

			if (child == 0) {		//子进程
				printf("connetion accepted! New client comming\n");

loop:
				memset(client_cmd, 0, sizeof(client_cmd));
				rval = 0;
				rval = read(msgsock, client_cmd, sizeof(client_cmd));
				if (rval < 0) {
					perror("reading command failed");
				}
				else if (rval == 0) {
					printf("connection closed.\n");
					close(datasock);
					close(msgsock);
				}
				else {				//解释命令
					if (strcmp(client_cmd, "pwd") == 0) {
						printf("command pwd\n");
						cmd_pwd(datasock);
						printf("done\n\n");
						goto loop;
					}
					else if (strcmp(client_cmd, "dir") == 0) {
						printf("command dir\n");
						cmd_dir(datasock);
						printf("done\n\n");
						goto loop;
					}
					else if (strcmp(client_cmd, "cd") == 0) {
						printf("command cd\n");
						memset(cmd_arg, 0, sizeof(cmd_arg));
						read(msgsock, cmd_arg, sizeof(cmd_arg));
						cmd_cd(datasock, cmd_arg);
						printf("done\n\n");
						goto loop;
					}
					else if (strcmp(client_cmd, "cd..") == 0) {
						printf("command cd ..\n");
						cmd_cdback(datasock);
						printf("done\n\n");
						goto loop;
					}
					else if (strcmp(client_cmd, "get") == 0) {
						printf("command get\n");
						memset(cmd_arg, 0, sizeof(cmd_arg));
						read(msgsock, cmd_arg, sizeof(cmd_arg));
						cmd_get(datasock, cmd_arg);
						printf("done\n\n");
						goto loop;
					}
					else if (strcmp(client_cmd, "put") == 0) {
						printf("command put\n");
						memset(cmd_arg, 0, sizeof(cmd_arg));
						read(msgsock, cmd_arg, sizeof(cmd_arg));
						cmd_put(datasock, cmd_arg);
						printf("done\n\n");
						goto loop;
					}
					else if (strcmp(client_cmd, "?") == 0) {
						printf("command ?\n");
						cmd_help(datasock);
						printf("done\n\n");
						goto loop;
					}
					else if (strcmp(client_cmd, "quit") == 0) {
						printf("quit\n");
						goto endchild;
					}
					else {
						printf("bad request!\n");
						goto loop;
					}
				}
endchild:
				printf("connection closed.\n");
				close(datasock);
				close(msgsock);
				exit(0);
			}
		}
	}while(TRUE);

	exit(0);
}
